<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="studyOrderSn">
        <el-input
          v-model="queryParams.studyOrderSn"
          placeholder="请输入学研订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医院名称" prop="hospitalName">
        <el-input
          v-model="queryParams.hospitalName"
          placeholder="请输入医院名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入用户名称/手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studyOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学研订单编号" align="center" prop="studyOrderSn" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #1c84c6;" @click="goDetail(scope.row)" v-hasPermi="['biz:studyOrder:query']">{{scope.row.studyOrderSn}}</span>
        </template>
      </el-table-column>
      <el-table-column label="学研项目" align="center" prop="projectName" />
      <el-table-column label="所属医院" align="center" prop="hospitalName" />
      <el-table-column label="订单数量" align="center" prop="num" />
      <el-table-column label="订单金额(陪护豆/个)" align="center" prop="amount" />
      <el-table-column label="用户" align="center" prop="userId" >
        <template slot-scope="scope">
          <span>{{scope.row.nickName}}({{scope.row.phonenumber}})</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
      :title="title"
      :visible.sync="open"
      size="45%"
      :show-close="true"
      append-to-body>
      <el-form ref="form" :model="form" label-width="130px" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单编号：">{{form.studyOrderSn}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="学研项目：">{{form.projectName}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="所属医院：">{{form.hospitalName}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单数量：">{{form.num}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单金额：">{{form.amount}}陪护豆</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户姓名：">{{form.nickName}}({{form.phonenumber}})</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="创建时间：">{{form.createTime}}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { listStudyOrder, getStudyOrder, delStudyOrder, addStudyOrder, updateStudyOrder, changeStatus } from "@/api/biz/studyOrder";

export default {
  name: "StudyOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学研订单表格数据
      studyOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studyOrderSn: undefined,
        projectId: undefined,
        projectName: undefined,
        hospitalName: undefined,
        hospitalId: undefined,
        num: undefined,
        amount: undefined,
        userId: undefined,
        keywords:undefined,
      },
      // 表单参数
      form: {},
      submitLoading: false,
      // 表单校验
      rules: {
        studyOrderSn: [
          { required: true, message: "学研订单编号不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "项目id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学研订单列表 */
    getList() {
      this.loading = true;
      listStudyOrder(this.queryParams).then(response => {
        this.studyOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studyOrderSn: null,
        projectId: null,
        hospitalId: null,
        num: null,
        amount: null,
        userId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学研订单";
    },
    handleChangeStatus(row) {
      let statusText = row.status === 'active' ? '禁用' : '启用'
      let status = row.status === 'active' ? 'inactive' : 'active'
      const ids = [row.id] || this.ids;
      this.$confirm('是否确认' + statusText + '该项数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeStatus({ids: ids, status: status});
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudyOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学研订单";
      });
    },
    goDetail(row){
      this.reset();
      const id = row.id || this.ids;
      getStudyOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "学研订单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id != null) {
            updateStudyOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          } else {
            addStudyOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学研订单编号为"' + ids + '"的数据项？').then(function() {
        return delStudyOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/studyOrder/export', {
        ...this.queryParams
      }, `studyOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
